/*
 * main.c
 *
 * 描述: 目前来说，这里的代码主要是用于对其他地方的代码进行简单的测试单元测试。
 *       为了测试方便，可能会进行一些不得体或者不兼容的操作，请勿随意照搬使用。
 * 
 * --------------------------------------------------------------------------
 *
 *      Copyright (c) 2021 LiuBingyi
 *      SimpleMark is licensed under Mulan PubL v2.
 *      You can use this software according to the terms and conditions of the Mulan PubL v2.
 *      You may obtain a copy of Mulan PubL v2 at:
 *               http://license.coscl.org.cn/MulanPubL-2.0
 *      THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 *      EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 *      MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 *      See the Mulan PubL v2 for more details.
 * 
 */

#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "Vector.h"
#include "basic_io.h"
#include "main.h"

void vectorEach(Vector v, constElementObj head, size_vector count, char* text)
{
    putSentence("***");
    putSentence(text);
    putSentenceln("START***");

    putSentence("v->elementCount: ");
    putNumber(v->elementCount);

    putSentence("\nv->elementSize: ");
    putNumber(v->elementSize);
    putSentenceln("");

    for (size_vector i = 0; i < count; i++) {
        putNumber(i);
        putSentence(":\t");
        printf("%I64d\n",*(long long*)(head + i * v->elementSize));
    }
    putSentence("****");
    putSentence(text);
    putSentenceln("END****\n");
}

int randPlace(Vector v, size_t count)
{
    size_vector l = vector_Method.length(v);
    for (size_t i = 0; i < count; i++) {
        long long r1 = rand() % l;
        long long position = rand() % l;
        vector_Method.place(v, &r1, position);
    }
    return 0;
}

int cycleAdd(size_vector count1, size_vector count2, size_vector placeCount)
{
    long long t = 4396;
    for (size_vector i = 0; i < count1; i++) {
        Vector vt = vector_Method.constructor(100, 100, sizeof(t));
        for (size_vector j = 0; j < count2; j++) {
            t += rand();
            vector_Method.add(vt, &t);
        }
        putSentenceln("-------------Cycle END-------------");
        vectorEach(vt,vector_Method.index(vt,0),vector_Method.length(vt),"Vector");
        randPlace(vt, placeCount);
#ifndef onClose
        vector_Method.close(vt);
#endif
    }
    return 0;
}

// int printVector(Vector vec)
// {
//     printf("vec->container=%p\n", vec->container);
//     printf("vec->capacity=%lld", vec->capacity);
//     printf("vec->capacityicrement=%lld\n", vec->capacityIncrement);
//     printf("vec->elementCount=%lld\n", vec->elementCount);
//     printf("vec->elementSize%ld\n", vec->elementSize);
//     return 0;
// }

int search()
{
    wchar_t a[] = L"美人自古如名将，不许人间见白头。";
    wchar_t search = L'见';
    Vector vec = vector_Method.constructor(10, 5, sizeof(a[0]));
    for (size_t i = 0; i < sizeof(a) / sizeof(wchar_t); i++) {
        vector_Method.add(vec, &a[i]);
    }
    for (size_t j = 0; j < vector_Method.length(vec); j++) {
        wprintf(L"索引%2d：%c\n", j, *(wchar_t*)vector_Method.index(vec, j));
    }
    size_vector position = vector_Method.indexOf(vec, &search);
    wprintf(L"搜索：%c\n", search);
    if (position >= vector_Method.length(vec))
        wprintf(L"找不到元素");
    else
        wprintf(L"位置：%d，值=%c\n", position, *(wchar_t*)vector_Method.index(vec, position));
    return 0;
    vector_Method.close(vec);
}

int inputSearch()
{

    wchar_t test[] = L"千古飞天梦，何日上九天？";
    Vector input = vector_Method.constructor(10, 10, sizeof(wchar_t));
    char* p;
    p = setlocale(LC_CTYPE, "");
    wprintf(L"%ls——冯石\n", test);
    wchar_t search;
    if (p == NULL) {
        printf("NULL\n");
    }
    wprintf(L"地域设置：");
    printf("%s\n", p);
    wprintf(L"输入要搜索的字符：");
    wscanf(L"%c", &search);
    printf("%#x\n", search);
    char clear;
    while ((clear = getchar()) == '\n' && clear == '\0') { }
    wprintf(L"输入被搜索的字符串：");

    wchar_t wc = L'\0';
    while (wc ^ '\n') {
        wc = getwchar();
        printf("0x%04x\n", wc);
        vector_Method.add(input, &wc);
    }
    //wprintf(L"1.%c\n", input);
    wprintf(L"输入=%ls\n", input->container);
    size_vector position = vector_Method.indexOf(input, &search);
    wprintf(L"搜索：%c\n", search);
    if (position >= vector_Method.length(input))
        wprintf(L"找不到元素");
    else
        wprintf(L"位置：%d，值=%c\n", position, *(wchar_t*)vector_Method.index(input, position));
    vector_Method.close(input);
    return 0;
}

int main(int argc, char const* argv[])
{
    inputSearch();
    //cycleAdd(1000,10000,1000);
    return 0;
}
